package com.gongpingjialoan.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.utility.DhUtil;
import com.gongpingjialoan.utility.ImageUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class LineChartView extends View {

    public int ylines;
    public int xlines;
    private Paint mPaint;
    int left = 160;
    int bottom = 120;
    int right = 80;
    int top = 40;
    String[] ytexts;
    String[] xtexts;
    //	int[][] xypoints={{1,20},{2,35},{3,45},{4,70},{5,100},{6,125},{7,160},{8,161},{9,180},{10,191},{11,200},{12,221},{13,240},{15,250},{16,270},{17,290},{18,308},{19,320},{20,330}};
    float[][] xypoints;
    int xmax = 5;
    int xmin = 0;
    float ymax = 5;
    float ymin = 0;
    int ywarning = 0;

    Paint paintCurve;

    Paint textPaint;

    private TextView tipV;
    Context context;

    int pointDrawable = 0;

    List<ClickPoints> clickpoints = new ArrayList<ClickPoints>();
    int linecolor;


    public LineChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        ylines = 4;
        xlines = 5;
//        String[] ytexts = {"22", "22.5", "25", "27.5", "30"};
//        String[] xtexts = {"5", "15", "20", "25", "30"};
//        this.xtexts = xtexts;
//        this.ytexts = ytexts;
//		tipV=new TextView(getContext());
//		tipV.setText("1212");
//		tipV.setBackgroundResource(R.drawable.chart_line_point_tip_red);
//		tipV.setPadding(DhUtil.dip2px(getContext(), 10), DhUtil.dip2px(getContext(), 5), DhUtil.dip2px(getContext(), 10), DhUtil.dip2px(getContext(), 5));
//		tipV.setTextColor(getResources().getColor(R.color.grey_dark));
//		tipV.setTextSize(12);
//		tipV.setVisibility(View.GONE);


        paintCurve = new Paint();
        paintCurve.setStyle(Paint.Style.STROKE);
        paintCurve.setDither(true);
        paintCurve.setAntiAlias(true);
        paintCurve.setStrokeWidth(DhUtil.dip2px(getContext(), 2));
        paintCurve.setColor(linecolor);
        textPaint = new Paint();
        textPaint.setColor(Color.BLACK);
        textPaint.setTextSize(DhUtil.sp2px(getContext(), 12));
//		addView(tipV);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (ytexts == null || pointDrawable == 0) {
            return;
        }
        mPaint.setStrokeWidth(1);
        float cellwidth = (getWidth() - left - right) / (float) xlines;
        float cellHeight = (getHeight() - bottom - top) / (float) ylines;
        mPaint.setColor(getResources().getColor(R.color.text_grey_dark));
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setTextSize(DhUtil.sp2px(getContext(), 10));


        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float fontTotalHeight = fontMetrics.bottom - fontMetrics.top;

//横线
        for (int i = 0; i < ylines; i++) {
            canvas.drawLine(left + 1, cellHeight * (i) + top, getWidth() - right - 1, cellHeight * (i) + top, mPaint);
            mPaint.setTextSize(DhUtil.dip2px(getContext(), 14));


//            if(i==0){
//                canvas.drawText(ytexts[i], 10, cellHeight * (i-1) + top + offY, textPaint);
//            } else {

//            }

        }


        float yuniform = (ymax - ymin) / (ylines );


        //画y轴文字
        for (int i = 0; i < ylines + 1; i++) {
            float offY = fontTotalHeight / 2 - fontMetrics.bottom;
            BigDecimal bg = new BigDecimal(ymax - yuniform * i).setScale(1,
                    BigDecimal.ROUND_HALF_UP);
            canvas.drawText(bg + "万", 30, cellHeight * (i) + top + offY, textPaint);
        }
//竖线
        mPaint.setColor(context.getResources().getColor(R.color.text_grey_dark));
        mPaint.setStrokeWidth(DhUtil.dip2px(getContext(), 1));
        for (int i = 0; i < xlines; i++) {
//			if(i==xlines-1){
//				canvas.drawLine(getWidth()-right-1, top,getWidth()-right-1,getHeight()-bottom, mPaint);
//			}else
            canvas.drawLine(left + cellwidth * (i + 1) - 1, getHeight() - bottom, left + cellwidth * (i + 1) - 1, getHeight() - 80, mPaint);
            mPaint.setTextSize(DhUtil.dip2px(getContext(), 14));
            canvas.drawText(xtexts[i], (left + cellwidth * (i + 1)) - cellwidth / 2 - textPaint.measureText(xtexts[i]) / 2, getHeight() - bottom + 50, textPaint);
        }
//        mPaint.setColor(Color.GRAY);
        mPaint.setTextSize(DhUtil.sp2px(getContext(), 10));

        //x轴
        canvas.drawLine(left, getHeight() - bottom - 1, getWidth() - right - 1, getHeight() - bottom - 1, mPaint);
        //y轴
        canvas.drawLine(left, getHeight() - bottom, left, getHeight() - 80, mPaint);


        float x = (getWidth() - left - right) / (float) (xmax - xmin);
        float y = (getHeight() - top - bottom) / (float) (ymax - ymin);
        Path path = new Path();
        //折线
        if (xypoints == null) {
            return;
        }
        for (int i = 0; i < xypoints.length; i++) {
            float[] p1 = null;
            if (i == 0) {
                float[] p = xypoints[0];
                p1 = p;
                path.moveTo(left + p1[0] * x, top + (ymax - p1[1]) * y);
            } else {

                //第一个点不需要画,应该直接将path move到了第一个点那
                if (i >= 2) {
                    p1 = xypoints[i - 1];
                    float[] p2 = xypoints[i - 2];
                    float startx = left + p1[0] * x;
                    float starty = top + (ymax - p1[1]) * y;
                    float lastx = left + p2[0] * x;
                    float lasty = top + (ymax - p2[1]) * y;
                    path.quadTo(lastx + 50, lasty, startx, starty);
                }


            }

            float[] p2 = xypoints[i];
            if (i == xypoints.length - 1) {
                float[] p3 = xypoints[i - 1];
                float lastx = left + p3[0] * x;
                float lasty = top + (ymax - p3[1]) * y;
                path.quadTo(lastx + 50, lasty, left + p2[0] * x, top + (ymax - p2[1]) * y);
//                path.lineTo(left + p2[0] * x, getHeight() - bottom);
//                path.close();
            }
//            canvas.drawLine(left + p1[0] * x, top + (ymax - p1[1]) * y, left + p2[0] * x, top + (ymax - p2[1]) * y, mPaint);
        }
        //折线下方
//		mPaint.setStyle(Paint.Style.FILL);
//		mPaint.setColor(Color.argb(127,95, 180, 227));
        canvas.drawPath(path, paintCurve);
        //重要点
        clickpoints.clear();
        int dotwidth = DhUtil.dip2px(getContext(), 4);
        for (int i = 0; i < xypoints.length; i++) {
            float[] p2 = xypoints[i];
//            if (p2[0] % 5 == 0) {
            Drawable draw = getResources().getDrawable(pointDrawable);
            Bitmap bit = ImageUtil.drawableToBitmap(draw);
            Rect src = new Rect(0, 0, bit.getWidth(), bit.getHeight());
            Rect dst = new Rect((int) (left + p2[0] * x - dotwidth), (int) (top + (ymax - p2[1]) * y - dotwidth), (int) (left + p2[0] * x + dotwidth), (int) (top + (ymax - p2[1]) * y + dotwidth));
            canvas.drawBitmap(bit, src, dst, null);
            ClickPoints point = new ClickPoints(left + p2[0] * x, top + (ymax - p2[1]) * y, ClickPoints.Type_Blue, p2[1] + "");
            clickpoints.add(point);

            canvas.drawText(p2[1] + "万", left + p2[0] * x - textPaint.measureText(xtexts[i]) / 2 + dotwidth / 2, top + (ymax - p2[1]) * y - 22, textPaint);

//            }
        }
        //
        mPaint.setColor(Color.rgb(247, 74, 101));
        float[] last = xypoints[xypoints.length - 1];
//        canvas.drawLine(left, top + (ymax - ywarning) * y, left + last[0] * x, top + (ymax - ywarning) * y, mPaint);
//		Drawable draw=getResources().getDrawable(R.drawable.chart_line_point_red);
//		Bitmap bit=ImageUtil.drawableToBitmap(draw);
//		Rect src = new Rect(0, 0, bit.getWidth(),bit.getHeight());
//		Rect dst = new Rect((int)(left+last[0]*x-dotwidth), (int)(top+(ymax-ywarning)*y)-dotwidth, (int)(left+last[0]*x+dotwidth), (int)(top+(ymax-ywarning)*y)+dotwidth);
//		canvas.drawBitmap(bit, src, dst, null);
//		ClickPoints point=new ClickPoints(left+last[0]*x, (int)(top+(ymax-ywarning)*y), ClickPoints.Type_Red,ywarning+"");
//		clickpoints.add(point);

    }


    public void setYtexts(String[] ytexts) {
        this.ytexts = ytexts;
//        invalidate();
    }

    public void setXtexts(String[] xtexts) {
        this.xtexts = xtexts;
//        invalidate();
    }

    public void setXypoints(float[][] xypoints) {
        this.xypoints = xypoints;
        invalidate();
    }

    public void setXmax(int xmax) {
        this.xmax = xmax;
        invalidate();
    }

    public void setYmax(float ymax, float ymin) {
        this.ymax = ymax;
        this.ymin = ymin;
//        invalidate();
    }

    public void setLineColor(int color) {
        this.linecolor = color;
        paintCurve.setColor(linecolor);
    }

    public void setPointDrawable(int pointDrawable) {
        this.pointDrawable = pointDrawable;
    }

    public void setYwarning(int ywarning) {
        this.ywarning = ywarning;
        invalidate();
    }

    public void setYLines(int ylines) {
        this.ylines = ylines;
        invalidate();
    }


    class ClickPoints {
        float x;
        float y;
        String text;
        int type;
        static final int Type_Red = 2;
        static final int Type_Blue = 1;

        public ClickPoints(float x, float y, int type, String text) {
            super();
            this.x = x;
            this.y = y;
            this.type = type;
            this.text = text;
        }

        private void onClick() {
//            tipV.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
//            LayoutParams params = (LayoutParams) tipV.getLayoutParams();
//            params.setMargins((int) x - 10, (int) y - tipV.getMeasuredHeight(), 0, 0);
//            tipV.setLayoutParams(params);
//            tipV.setText(text);
//            tipV.setVisibility(View.VISIBLE);

        }

        public boolean check(float x, float y) {
            return Math.abs(this.x - x) < 20 && Math.abs(this.y - y) < 20;
        }

    }


}
